<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,200分)- 硬件产品销售方案（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某公司目前推出了AI开发者套件&#xff0c;AI加速卡&#xff0c;AI加速模块&#xff0c;AI服务器&#xff0c;智能边缘多种硬件产品&#xff0c;每种产品包含若干个型号。<br /> 现某合作厂商要采购金额为<em>amount</em>元的硬件产品搭建自己的AI基座。<br /> 例如当前库存有<em>N</em>种产品&#xff0c;每种产品的库存量充足&#xff0c;给定每种产品的价格&#xff0c;记为price&#xff08;不存在价格相同的产品型号&#xff09;。<br /> 请为合作厂商列出所有可能的产品组合。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入包含采购金额amount和产品价格列表price。第一行为amount&#xff0c;第二行为price&#xff0c;例如&#xff1a;</p> 
<p>500<br /> [100, 200, 300, 500]</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出为组合列表。例如&#xff1a;</p> 
<p>[[100, 100, 100, 100, 100], [100, 100, 100, 200], [100, 100, 300], [100, 200, 200], [200, 300], [500]]</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">500<br /> [100, 200, 300, 500, 500]</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">[[100, 100, 100, 100, 100], [100, 100, 100, 200], [100, 100, 300], [100, 200, 200], [200, 300], [500], [500]]</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的可重复元素组合求解。</p> 
<hr /> 
<p>2023.08.01</p> 
<p>更新了JS代码的输出格式逻辑&#xff0c;即在逗号后面加空格</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const amount &#61; lines[0] - 0;
    const prices &#61; JSON.parse(lines[1]);

    console.log(getResult(amount, prices));
    lines.length &#61; 0;
  }
});

function getResult(amount, prices) {
  const res &#61; [];
  dfs(amount, prices, 0, 0, [], res);
  return &#96;[${res.join(&#34;, &#34;)}]&#96;;
}

function dfs(total, arr, index, sum, path, res) {
  if (sum &gt;&#61; total) {
    if (sum &#61;&#61;&#61; total) res.push(&#96;[${path.join(&#34;, &#34;)}]&#96;);
    return;
  }

  for (let i &#61; index; i &lt; arr.length; i&#43;&#43;) {
    path.push(arr[i]);
    dfs(total, arr, i, sum &#43; arr[i], path, res);
    path.pop();
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int amount &#61; Integer.parseInt(sc.nextLine());

    String str &#61; sc.nextLine();
    Integer[] prices &#61;
        Arrays.stream(str.substring(1, str.length() - 1).split(&#34;, &#34;))
            .map(Integer::parseInt)
            .toArray(Integer[]::new);

    System.out.println(getResult(amount, prices));
  }

  public static String getResult(int amount, Integer[] prices) {
    ArrayList&lt;String&gt; res &#61; new ArrayList&lt;&gt;();
    LinkedList&lt;Integer&gt; path &#61; new LinkedList&lt;&gt;();

    dfs(amount, prices, 0, 0, path, res);
    return res.toString();
  }

  public static void dfs(
      int total,
      Integer[] arr,
      int index,
      int sum,
      LinkedList&lt;Integer&gt; path,
      ArrayList&lt;String&gt; res) {
    if (sum &gt;&#61; total) {
      if (sum &#61;&#61; total) res.add(path.toString());
      return;
    }

    for (int i &#61; index; i &lt; arr.length; i&#43;&#43;) {
      path.addLast(arr[i]);
      dfs(total, arr, i, sum &#43; arr[i], path, res);
      path.removeLast();
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python">amount &#61; int(input())
prices &#61; eval(input())


def dfs(total, arr, index, sum, path, res):
    if sum &gt;&#61; total:
        if sum &#61;&#61; total:
            res.append(path[:])
        return

    for i in range(index, len(arr)):
        path.append(arr[i])
        dfs(total, arr, i, sum &#43; arr[i], path, res)
        path.pop()


res &#61; []
dfs(amount, prices, 0, 0, [], res)
print(res)
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>